﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class CTPhieuNhapHangDAO
    {
        public List<CTPhieuNhapHangDTO> HienThi(string mapn)
        {
            string sql = "select MACTPN, MAMH, SOLUONG, CAST(DONGIA as int) as DonGiaMoi from CTPHIEUNHAP where MAPN=";
            sql += "'" + mapn + "'";
            SqlConnection conn = DataProvider.ConnectionDB();
            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataReader reader = command.ExecuteReader();

            List<CTPhieuNhapHangDTO> danhsach = new List<CTPhieuNhapHangDTO>();
            while (reader.Read())
            {
                CTPhieuNhapHangDTO dto = new CTPhieuNhapHangDTO();
                dto.MACTPN = reader.GetString(0);
                dto.MAMH = reader.GetString(1);
                dto.SOLUONG = reader.GetInt32(2);
                dto.DONGIA = reader.GetInt32(3);
                danhsach.Add(dto);
            }
            conn.Close();
            return danhsach;
        }

        public int ThemCTPNH(CTPhieuNhapHangDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "insert into CTPHIEUNHAP values(";
            sql += "N'" + dto.MACTPN + "',";
            sql += "N'" + dto.MAPN + "',";
            sql += "N'" + dto.MAMH + "',";
            sql += "'"+dto.SOLUONG+"',";
            sql += dto.DONGIA;
            sql += ")";

            SqlCommand command = new SqlCommand(sql, conn);
            int kq = command.ExecuteNonQuery();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        public int CapNhat(CTPhieuNhapHangDTO dto)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "update CTPHIEUNHAP ";
            sql += "set MAPN=";
            sql += "N'" + dto.MAPN + "',";
            sql += "MAMH=";
            sql += "N'" + dto.MAMH + "',";
            sql += "SOLUONG=";
            sql += dto.SOLUONG;
            sql += ",";
            sql += "DONGIA=";
            sql += dto.DONGIA;
            sql += " where MACTPN=";
            sql += "'" + dto.MACTPN + "'";

            SqlCommand command = new SqlCommand(sql, conn);
            int kq = command.ExecuteNonQuery();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }

        public int KiemTraMACTPN(string MACTPN)
        {
            SqlConnection conn = DataProvider.ConnectionDB();
            string sql = "Select count(*) from CTPHIEUNHAP where MACTPN=";
            sql += "'" + MACTPN + "'";
            SqlCommand command = new SqlCommand(sql, conn);
            int kq = (int)command.ExecuteScalar();
            conn.Close();
            if (kq <= 0)
                return 0;
            return 1;
        }
    }
}
